<?php
namespace Admin\Controller;
use Think\Verify;
use Think\Controller;

class IndexController extends Controller 
{
    public function __construct()  
    {  
        parent::__construct();  
        $res = isLogin();

        if($res == '1')
        {
            $this -> error('还未登录',__APP__.'/Admin/Login/index');
        }
        $returnPoints = returnPoints();
    }

	/*登录首页*/
    public function index()
    {
    	$arr['user_Id'] = session('user_id');
    	$UserModer = D('User');
		$res       = $UserModer->where($arr)->select();

        //获取可返积分单元
        $weightModer = D('Weight');
        $weightRes   = $weightModer->num($arr['user_Id']);
        $num = count($weightRes);

    	//获取日志条数
		$LogModer = D('Log');
		$logRes = $LogModer->index($arr['user_Id']);
		$length = count($logRes);

        //获取提现记录
        $payModer = D('Pay');
        $payRes   = $payModer->num($arr['user_Id']);
        $payRes1   = $payModer->numOne($arr['user_Id']);
        $payRes2   = $payModer->numTwo($arr['user_Id']);
        $payNum = count($payRes)+count($payRes1)+count($payRes2);
		//获取权重
		$weight = C('WEIGHT');

		$this->assign('weight', $weight);
		$this->assign('length', $length);
    	$this->assign('user', $res);
        $this->assign('num', $num);
        $this->assign('payNum', $payNum);
        $this->display();
    }
    /*退出*/
    public function seigOut()
    {
    	session('user_id', 0);

        $this->success('已退出登录！', __APP__.'/Admin/Login/index');
    }

    /*可返单元*/
    public function weight()
    {
        $arr['user_Id'] = session('user_id');
        //获取可返积分单元
        $weightModer = D('Weight');
        $weightRes       = $weightModer->num($arr['user_Id']);
        //获取用户信息
        $UserModer = D('User');
        $res       = $UserModer->where($arr)->select();

        $this->assign('weightRes', $weightRes);
        $this->assign('res', $res);
        $this->display();
    }

    /*提现记录*/
    public function pay()
    {
        $arr['user_Id'] = session('user_id');
        //获取审核不通过的记录
        $payModer = D('Pay');
        $payRes1   = $payModer->numTwo($arr['user_Id']);
        //获取审核成功的记录
        $payRes2  = $payModer->numOne($arr['user_Id']);
        //获取已提现记录
        $payRes   = $payModer->num($arr['user_Id']);
        
        $this->assign('payRes1', $payRes1);
        $this->assign('payRes2', $payRes2);
        $this->assign('payRes', $payRes);
        $this->display();
    }

    /*验证码*/
    public function miti()
    {
    	$Verify = new \Think\Verify();
		$Verify->fontSize = 30;
		$Verify->length   = 4;
		$Verify->useNoise = false;
		$Verify->entry();
    }

    /*密码修改*/
    public function reset($pwd = ' ')
    {
        $this->display();
    }

     /*确认密码修改*/
    public function resetIn()
    {
        $arr['user_Id'] = session('user_id');
        $arr['oldPwd'] = md5(I('post.oldPwd'));
        $arr['newPwd1'] = md5(I('post.newPwd1'));
        $arr['newPwd2'] = md5(I('post.newPwd2'));
        $arr['check'] = I('post.check');

        $userModel = D('User');
        $userRes = $userModel->pwd($arr);

        if($userRes == 'onEq') 
        {
            $this -> error('验证码不正确！');
        }elseif($userRes == 'error')
        {
            $this->error('两次新密码不一样！');
        }elseif($userRes == 'oldErr')
        {
            $this -> error('旧密码填写错误！');
        }elseif($userRes == 'err')
        {
            $this -> error('密码修改失败！',  __APP__.'/Admin/Index/index');
        }elseif($userRes == 'success')
        {
            $this -> success('密码修改成功！',  __APP__.'/Admin/Index/index');
        }

        
    }

    /*积分日志*/
    public function pay_points()
    {
    	$arr['user_Id'] = session('user_id');

    	$UserModer = M('Log');
		$res       = $UserModer->where($arr)->select();

		$this->assign('pointsRes', $res);
		$this->display();
    }

    /*我是商家*/
    public function shop()
    {
        $arr['user_Id'] = session('user_id');

        $UserModer = D('User');
        $res_shop  = $UserModer->shop($arr['user_Id']);

        $this->assign('shop', $res_shop);
        $this->display();
    }

    /*我的粉丝*/
    public function fans()
    {
        $arr['user_Id'] = session('user_id');

        $UserModer = D('User');
        $res_user  = $UserModer->myfans($arr['user_Id'], 'user');
        $res_shop  = $UserModer->myfans($arr['user_Id'], 'shop');
        //var_dump($res);exit;

        $this->assign('userfans', $res_user);
        $this->assign('shopfans', $res_shop);
        $this->display();
    }

    /*消费积分*/
    public function xiaofei()
    {
        $arr['user_Id'] = session('user_id');
        $arr['reason'] = '消费积分';

        $UserModer = M('Log');
        $res       = $UserModer->where($arr)->select();

        $this->assign('res', $res);
        $this->display();
    }

    /*赠送积分*/
    public function zengsong()
    {
        $arr['user_Id'] = session('user_id');
        $arr['reason'] = '赠送积分';

        $UserModer = M('Log');
        $res       = $UserModer->where($arr)->select();

        $this->assign('res', $res);
        $this->display();
    }

    /*我要提现*/
    public function tixian()
    {
        $this->display();
    }
    /*发起提现申请*/
    public function cash()
    {
        $pay['user_id'] = session('user_id');
        $pay['number']   = trim(I('post.number'));

        $payModer = D('Pay');
        $res       = $payModer->cash($pay);

        if($res == 'in')
        {
            $this->success('已成功发起申请！', __APP__.'/Admin/Index/index');
        }elseif ($res == 'no') 
        {
            $this->error('发起申请失败！',  __APP__.'/Admin/Index/index');
        }elseif ($res == 'noNum') 
        {
            $this->error('返还积分达到100是才可提现！',  __APP__.'/Admin/Index/index');
        }
    }

    //管理银行卡
    public function bankCard()
    {
        $arr['user_Id'] = session('user_id');
        $UserModer = D('User');
        $res       = $UserModer->where($arr)->select();

        $this->assign('res', $res);
        $this->display();
    }

     /*确认密码修改*/
    public function bankCardIn()
    {
        $arr['user_Id'] = session('user_id');
        $arr['banktype'] = trim(I('post.bankPlace'));
        $arr['card'] = trim(I('post.card'));

        $userModel = D('User');
        $userRes = $userModel->bankCardIn($arr);

        if($userRes == 'success') 
        {
            $this -> success('修改成功！', __APP__.'/Admin/Index/index');
        }elseif($userRes == 'oldErr')
        {
            $this -> error('用户不存在！', __APP__.'/Admin/Index/index');
        }elseif($userRes == 'err')
        {
            $this -> error('修改失败！', __APP__.'/Admin/Index/index');
        }

        
    }
	
	//推荐二维码
	public function tjcode(){
		$n_url = 'http://'.$_SERVER['HTTP_HOST'].'/index.php/Admin/Login/register/uid/'.session('user_id');
		$url = "http://qr.liantu.com/api.php?text=".$n_url;
		$this -> assign('url',$url);
		$this -> display();
	}
	

}